import React, { PureComponent, createRef, forwardRef } from "react";

const HelloWorld = forwardRef(function (props, ref) {
  // function test(){
  //   console.log("test子组件-------")
  // }
  return <h1 ref={ref}>helloworld子组件函数式</h1>;
});

export class App extends PureComponent {
  constructor() {
    super();
    this.state = {};
    this.HelloWorldRef = createRef();
  }
  getComponent() {
    // HelloWorldInstance.test()
    console.log(this.HelloWorldRef.current);
    this.HelloWorldRef.current.test();
  }
  render() {
    return (
      <div>
        <HelloWorld ref={this.HelloWorldRef}></HelloWorld>
        <button onClick={(e) => this.getComponent()}>获取组件实例</button>
      </div>
    );
  }
}

export default App;
